<form id="form1" method="post">
	<input name="cid" id="cid" class="mini-hidden" value="0"/>
	<input name="sid" id="sid" class="mini-hidden" value="0"/>
	<input name="types" id="types" class="mini-hidden" value=""/>
	<div style="padding-left:11px;padding-bottom:5px;">
		<table style="table-layout:fixed;width:100%;">
			<tr style="height:25px;">
				<td style="width:100px;">员工编号：</td>
				<td style="width:200px;"><input name="user" id="user" required="true" vtype="minLength:2" emptyText="请输入登录帐号！" class="mini-textbox" style="width:100%;"/></td>
				<td style="width:420px;height:320px;" rowspan="10">
					<div id="datagrid1" idField="id" showFooter="false" multiSelect="true" class="mini-datagrid" style="width:85%;height:100%;" url="/staff/role">
					    <div property="columns">
					        <div type="checkcolumn"></div>
					        <div name="name" field="name" width="250px">权限角色名称</div>
					    </div>
					</div>
				</td>
			</tr>
			<tr style="height:22px;">
				<td>登录密码：</td>
				<td><input name="password" id="password" required="true" emptyText="请输入密码！" class="mini-textbox" style="width:100%;"/></td>
			</tr>
			<tr style="height:22px;">
				<td>部门机构：</td>
				<td><input name="agency" id="agency" required="true" emptyText="请输入部门机构！" expandOnLoad="true" class="mini-treeselect" url="/common/agencyOne" multiSelect="false" textField="name" valueField="id" parentField="parent" checkRecursive="true" showFolderCheckBox="true" style="width:100%;"/></td>
			</tr>
			<tr style="height:22px;">
				<td>员工职位：</td>
				<td><input name="position" id="position" required="true" emptyText="请输入职位！" class="mini-combobox" textField="name" valueField="id" allowInput="true" style="width:100%;"/></td>
			</tr>
			<tr style="height:25px;">
				<td>员工真实姓名：</td>
				<td><input name="name" id="name" required="true" vtype="minLength:2" emptyText="请输入真实姓名！" class="mini-textbox" style="width:100%;"/></td>
			</tr>
			<tr style="height:25px;">
				<td>员工性别：</td>
				<td><input name="sex" id="sex" value="未知" class="mini-radiobuttonlist" data="[{id:'男',text:'男'},{id:'女',text:'女'}]"/></td>
			</tr>
			<tr style="height:25px;">
				<td>员工手机：</td>
				<td><input name="mobile_phone" id="mobile_phone" emptyText="请输入手机！" class="mini-textbox" style="width:100%;"/></td>
			</tr>
			<tr style="height:25px;">
				<td>员工座机：</td>
				<td><input name="tel" id="tel" emptyText="请输入座机！" class="mini-textbox" style="width:100%;"/></td>
			</tr>
			<tr style="height:25px;">
				<td>员工邮箱：</td>
				<td><input name="email" id="email" vtype="email" emptyText="请输入邮箱！" class="mini-textbox" style="width:100%;"/></td>
			</tr>
			<tr style="height:25px;">
				<td>备注：</td>
				<td><input name="remark" id="remark" class="mini-textarea" style="width:100%;height:60px;" emptyText="请输入描述"/></td>
			</tr>
		</table>
	</div>
	<div style="text-align:center;padding:10px;">   
		<a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>
		<a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>
	</div>
</form>
<script type="text/javascript">
mini.parse();
var grid = mini.get("datagrid1");
var position = mini.get("position");
var form = new mini.Form("form1");
grid.load();

//咨询投诉区分
mini.get("agency").on("valuechanged",function(e){
	position.load("/staff/Position?agency=" + e.value);
});

function SaveData() {
	form.validate();
	if (form.isValid() == false) return;
	var rows = grid.getSelecteds();
	//console.log(rows);
    if (rows.length > 0) {
		var id = [];
		for(var i=0;i<rows.length;i++){
			id.push(rows[i].id);
		}
		var role = id.join(',');
	}

    if(mini.get("types").getValue()=="add"){
		$.ajax({
			type:"POST",
			url:"/staff/userAdd",
			data:{data:form.getData(),role:role},
			cache: false,
			dataType: "json",
			success: function (text) {
				//console.log(text);
				if(text.code=="MG"){
					parent.parent.$.dooDialog.alert(text.msg);
				}else if(text.code=="NG"){
					parent.parent.$.dooDialog.alert("保存失败！");
				}else{
					CloseWindow("save");
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				CloseWindow();
			}
		});
    }else{
    	$.ajax({
			type:"POST",
			url:"/staff/userUpd",
			data:{data:form.getData(),role:role},
			cache: false,
			dataType: "json",
			success: function (text) {
				if(text.code=="NG"){
					parent.parent.$.dooDialog.alert("保存失败！");
				}else{
					CloseWindow("save");
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				CloseWindow();
			}
		});
    }
}

function SetData(data) {
	mini.get("types").setValue(data.action);
	if (data.action == "edit") {
		data = mini.clone(data);
		mini.get("password").disable();
		mini.get("user").disable();
		mini.get("name").disable();
		mini.get("password").setValue("******");
		mini.get("cid").setValue(data.cid);
		mini.get("sid").setValue(data.sid);
		mini.get("user").setValue(data.user);
		mini.get("agency").setValue(data.agency);
		mini.get("position").setValue(data.position_id);
		mini.get("position").setText(data.position);
		mini.get("name").setValue(data.name);
		mini.get("sex").setValue(data.sex);
		mini.get("mobile_phone").setValue(data.mobile_phone);
		mini.get("tel").setValue(data.tel);
		mini.get("email").setValue(data.email);
		mini.get("remark").setValue(data.remark);
		setTimeout("statusload()",300);
	}
}

function CloseWindow(action) {
	if (action == "close" && form.isChanged()) {
		if (confirm("数据被修改了，是否先保存？")) {
			return false;
		}
	}
	if (window.CloseOwnerWindow){
		return window.CloseOwnerWindow(action);
	}else{
		window.close();
	}
}

function onOk(e) {
	SaveData();
}
function onCancel(e) {
	CloseWindow("cancel");
}

function statusload(){
	for(var i=0;i<JS.role.length;i++){
		grid.select(JS.role[i]);
	}
}
</script>